package com.jt.common.util;


public class LngLatUtil {

	private static double EARTH_RADIUS = 6378.137;

	public static void main(String[] args) {
		// 比如：点A的经度为11695400，纬度为3995400。点B的经度为11695300，纬度为3995300。
		
		System.out.println(distance(116.433284,39.920528, 116.4344877,39.9200319));
		
		System.out.println(distance(116.433284,39.920528, 116.4258477,39.9184077));
	}
	
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 计算地球上任意两点(经纬度)距离
	 * 
	 * @param long1
	 *            第一点经度
	 * @param lat1
	 *            第一点纬度
	 * @param long2
	 *            第二点经度
	 * @param lat2
	 *            第二点纬度
	 * @return 返回距离 单位：米
	 */
	public static double distance(double long1, double lat1, double long2, double lat2) {
		double a, b, R;
		R = 6378137; // 地球半径
		lat1 = lat1 * Math.PI / 180.0;
		lat2 = lat2 * Math.PI / 180.0;
		a = lat1 - lat2;
		b = (long1 - long2) * Math.PI / 180.0;
		double d;
		double sa2, sb2;
		sa2 = Math.sin(a / 2.0);
		sb2 = Math.sin(b / 2.0);
		d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
		return d;
	}

	/**
	 * 根据经纬度和半径计算经纬度范围
	 * 
	 * @param raidus
	 *            单位米 return minLng,minLat,maxLng, maxLat
	 */
	public static double[] getAround(double lon, double lat, int raidus) {

		Double latitude = lat;
		Double longitude = lon;

		Double degree = (24901 * 1609) / 360.0;
		double raidusMile = raidus;

		Double dpmLat = 1 / degree;
		Double radiusLat = dpmLat * raidusMile;
		Double minLat = latitude - radiusLat;
		Double maxLat = latitude + radiusLat;

		Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
		Double dpmLng = 1 / mpdLng;
		Double radiusLng = dpmLng * raidusMile;
		Double minLng = longitude - radiusLng;
		Double maxLng = longitude + radiusLng;
		return new double[] { minLng, maxLng, minLat, maxLat };
	}
	
	/**
	 * 米转换成千米
	 * @param m
	 * @return
	 */
	public static double mToKm(Integer m){
		double dis = 0;
		dis = Math.round(m/100d)/10d;
		return dis;
	}
	
	public static String getDirectionStr(Integer direction){
		
		if (direction == null) {
			return "无";
		} else if (0 < direction && direction < 90) {
			return "东北";
		} else if (90 < direction && direction < 180) {
			return "东南";
		} else if (180 < direction && direction < 270) {
			return "西南";
		} else if (270 < direction && direction < 360) {
			return "西北";
		} else if (0 == direction) {
			return "北";
		} else if (90 == direction) {
			return "东";
		} else if (180 == direction) {
			return "南";
		} else if (270 == direction) {
			return "西";
		}
		return "无";
	}

}
